package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.BuildConfig;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.constant.Type;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.ArticleModel;
import com.cheer.ba.model.PointModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.model.UserInfoModel;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 登录处理器
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class LoginPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private EntityCache<UserInfoModel> mUserInfoCache;
    private APIService mService;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserInfoCache = new EntityCache<UserInfoModel>(mContext, UserInfoModel.class, "");
        mService = Http.getUserService(mContext);
    }


    /**
     * 注册大礼包
     */
    public void registerGift() {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "");
            return;
        }
        Call<ResponseData> call = mService.getUserRegisterGift(1003, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.REGISTER_GIFT);
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
            }
        });
    }

    /**
     * 获得用户登录信息
     */
    public void login(String mPhone, String mpw) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.login_loading));
        Call<ResponseData<UserInfoModel>> call = mService.login(1003, mPhone, mpw);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<UserInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseData<UserInfoModel>> call, Response<ResponseData<UserInfoModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<UserInfoModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        UserInfoModel userModel = res.getData();
                        if (null != userModel) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                            mView.updateView(userModel);
                            return;
                        }
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.login_fail));
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.login_fail));
            }

            @Override
            public void onFailure(Call<ResponseData<UserInfoModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.login_fail));
            }
        });
    }

    /**
     * 获取手机短信验证码
     *
     * @param mPhone
     * @param type   1注册 2忘记密码 6绑定银行卡
     */
    public void getVeriCode(String mPhone, String type, String custId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        Call<ResponseData<UserInfoModel>> call = mService.getSms(1003, custId, mPhone, type);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<UserInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseData<UserInfoModel>> call, Response<ResponseData<UserInfoModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<UserInfoModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
//                        UserInfoModel userModel = res.getData();
//                        if (null != userModel) {
                        mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
//                            mView.updateView(userModel);
                        return;
//                        }
                    } else {
//                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.get_code_error));
                        mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, res.getMsg());
                        return;
                    }
                }
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.get_code_error));
            }

            @Override
            public void onFailure(Call<ResponseData<UserInfoModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.get_code_error));
            }
        });
    }

    /**
     * 注册用户
     *
     * @param phone    电话
     * @param password 密码
     * @param veriCode 验证码
     * @author HQ
     * created at 2017/3/8 0008 13:49
     */
    public void registerUser(String phone, String password, String veriCode, String mChannel, String member) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.register_loading));
        Call<ResponseData<UserInfoModel>> call = mService.register(
                1002,
                phone,
                password,
                veriCode, "4",
                mChannel, member);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<UserInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseData<UserInfoModel>> call, Response<ResponseData<UserInfoModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<UserInfoModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        UserInfoModel user = res.getData();
                        if (null != user) {
                            //注册成功
                            Session.setLoginWay(Type.LOGIN_PHONE);
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, mContext.getString(R.string.register_success));
                            mView.updateView(user);
                            return;
                        }
                    } else {
                        //注册失败
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.register_fail));
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.register_fail));
            }

            @Override
            public void onFailure(Call<ResponseData<UserInfoModel>> call, Throwable t) {
                super.onFailure(call, t);
                //注册失败
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.register_fail));
            }
        });
    }

    /**
     * 重置登录密码
     *
     * @param phone    电话
     * @param password 密码
     * @param veriCode 验证码
     * @author HQ
     * created at 2017/3/8 0008 13:49
     */
    public void forgotPw(String phone, String password, String veriCode) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.reset_pw_loading));
        Call<ResponseData> call = mService.resetPw(
                1002,
                phone,
                password,
                veriCode
        );
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        //重置登录密码成功
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, mContext.getString(R.string.modify_pw_success));
                        mView.updateView(res.getRet());
                        return;
                    } else {
                        //重置登录密码失败
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.reset_pw_fail));
                        return;
                    }
                }
                //重置登录密码失败
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.reset_pw_fail));
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                //重置登录密码失败
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.reset_pw_fail));
            }
        });
    }

    /**
     * 修改登录密码
     *
     * @param oldPw 旧密码
     * @param newPw 新密码
     * @author HQ
     * created at 2017/3/8 0008 13:49
     */
    public void modifyLoginPw(String oldPw, String newPw) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.modify_login_loading));
        Call<ResponseData> call = mService.modifyPw(1002, Session.getUserId(), oldPw, newPw);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        //修改登录密码成功
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, mContext.getString(R.string.modify_login_pw_success));
                        mView.updateViewWithTag(res.getRet(), ConstantTag.MODIFY_LOGIN_PW);
                        return;
                    } else {
                        //修改登录密码失败
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.modify_login_pw_fail));
                        return;
                    }
                }
                //修改登录密码失败
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.modify_login_pw_fail));
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                //修改登录密码失败
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.modify_login_pw_fail));
            }
        });
    }

    /**
     * 签到
     */
    public void userSign() {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "");
        Call<ResponseData<PointModel>> call = mService.userSign(1003, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<PointModel>>() {
            @Override
            public void onResponse(Call<ResponseData<PointModel>> call, Response<ResponseData<PointModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<PointModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        PointModel userModel = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("签到成功", ConstantTag.SIGN_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "签到失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "签到失败");
            }

            @Override
            public void onFailure(Call<ResponseData<PointModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "签到失败");
            }
        });
    }

    @Override
    public void detachView() {
    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
